package com.baizhi.controller;

import com.baizhi.feign.GoodsServiceFeign;
import com.baizhi.pojo.Count;
import com.baizhi.pojo.Goods;
import com.baizhi.constant.MessageConstant;
import com.baizhi.entity.PageResult;
import com.baizhi.entity.QueryPageBean;
import com.baizhi.entity.Result;
import com.baizhi.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/backend/goods")
public class GoodsController {

    @Autowired
    private GoodsServiceFeign goodsServiceFeign;

  /*  *//**
     * 查询分页
     *
     * @param page
     * @param rows
     * @return
     *//*
    @RequestMapping("selectGoodsByPage")
    public Result selectGoodsByPage(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "2") Integer rows) {
        PageResult pageResult = goodsServiceFeign.selectGoodsByPage(page, rows);
        if (pageResult.getResult() != null && pageResult.getResult().size() > 0) {
            return Result.ok(pageResult);
        } else {
            return Result.error("查无结果");
        }
    }

    *//**
     * 添加
     *
     * @param goods
     * @return
     *//*
    @RequestMapping("insertGoods")
    public Result insertGoods(Goods goods) {
        Integer insertGoodsNum = goodsServiceFeign.insertGoods(goods);
        if (insertGoodsNum > 0) {
            return Result.ok();
        }
        return Result.error("添加失败");
    }

    *//**
     * 删除
     * @param id
     * @return
     *//*
    @RequestMapping("deleteGoodsById")
    public Result deleteGoodsById(Long id){
        Integer num=goodsServiceFeign.deleteGoodsById(id);
        if (num>0){
            return Result.ok();
        }
        return Result.error("删除失败");
    }*/

    /**
     * 查询分页
     * @param queryPageBean
     * @return
     */
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        PageResult pageResult = goodsServiceFeign.findPage(queryPageBean);
        return pageResult;
    }

    /**
     * 添加
     * @param goods
     * @return
     */
    @RequestMapping("/insertGoods")
    public Result insertGoods(@RequestBody Goods goods){
        try {
            goodsServiceFeign.insertGoods(goods);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADDFAIL);
        }
        return new Result(true, MessageConstant.ADDSUCCESS);
    }


    /**
     * 根据id查询
     * @param id
     * @return
     */
    @RequestMapping("findById")
    public Result findById(Long id){
        try {
            Goods goods =goodsServiceFeign.findById(id);
            return new Result(true, MessageConstant.QUERYSUCCESS,goods);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERYFAIL);
        }

    }
    /**
     * 修改
     * @param goods
     * @return
     */
    @RequestMapping("/edit")
    public Result edit(@RequestBody Goods goods){
        try {
            Integer num = goodsServiceFeign.edit(goods);
            if (num>0){
                return new Result(true, MessageConstant.EDITSUCCESS);
            }
            return new Result(false, MessageConstant.EDITFAIL);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.EDITFAIL);
        }

    }

    /**
     * 删除
     * @param id
     * @return
     */
    @RequestMapping("delete")
    public Result delete(Long id){
        try {
            Integer num = goodsServiceFeign.delete(id);
            if (num>0){
                return new Result(true, MessageConstant.DELETESUCCESS);
            }
            return new Result(false, MessageConstant.DELETEFAIL);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETEFAIL);
        }

    }

    /**
     * 修改状态
     * @param id
     * @param status
     * @return
     */
    @RequestMapping("editStatus")
    public Result editStatus(Long id,Integer status){
        Integer num = goodsServiceFeign.editStatus(id,status);
        if (num>0){
            return new Result(true,"成功");
        }
        return new Result(false,"失败");
    }

    /**
     * 状态因为
     * @param id
     * @param status
     * @param b
     * @return
     */
    @RequestMapping("/updateStatusAndBecause")
    public Result updateStatusAndBecause(Integer id,Integer status,String b){
        Integer num = goodsServiceFeign.updateStatusAndBecause(id,status,b);
        if (num>0){
            return new Result(true,"成功");
        }
        return new Result(false,"失败");
    }

    /**
     * 统计图
     * @return
     */
    @RequestMapping("/count")
    public Result count(){
            List<Count> list =goodsServiceFeign.count();
        if (list.size()>0){
            return new Result(true,"成功");
        }
        return new Result(false,"失败");
    }
}
